package com.chenfan.mcn.extension.log;

import com.chenfan.mcn.config.UserVoContextHolder;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.ObjectUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * @author tangwei
 * Date:     2021/1/9 17:27
 * Description:
 */
@Data
@Accessors(chain = true)
public class FieldChangeDTO<T> implements Serializable {

    /**
     * traceId
     */
    private String traceId;

    /**
     * 行为名称
     */
    private String actionName;

    /**
     * 业务ID
     */
    private Long businessId;

    /**
     * 业务类型
     */
    private Integer businessType;

    /**
     * 行为备注
     */
    private String remark;

    /**
     * 操作类型
     */
    private Integer operationType;

    /**
     * 操作前对象
     */
    private T beforeValue;

    /**
     * 操作后对象
     */
    private T afterValue;

    /**
     * 操作时间
     */
    private Date operationTime;

    /**
     * 操作人id
     */
    private Long operationBy;

    /**
     * 操作人名称
     */
    private String operationName;

    public FieldChangeDTO() {
        this(null, null, null);
    }

    public FieldChangeDTO(Long operationBy, String operationName) {
        this(operationBy, operationName, null);
    }

    public FieldChangeDTO(Long operationBy, String operationName, Date operationTime) {
        this.operationBy = ObjectUtils.defaultIfNull(operationBy, UserVoContextHolder.getUserId());
        this.operationName = ObjectUtils.defaultIfNull(operationName, UserVoContextHolder.getUserName());
        if(operationTime == null) {
            this.operationTime = new Date();
        } else {
            this.operationTime = operationTime;
        }
    }
}